package com.ty.day4;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * Created by IntelliJ IDEA.
 *
 * @author : txy
 * @create : 2023/11/27 15:49
 */
public class CompaebaleDemo01 {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("居居波",25));
        students.add(new Student("吉儿阳",22));
        students.add(new Student("唐小宇",23));
        students.add(new Student("小彭友",20));
        Collections.sort(students);
        System.out.println(students);
        Collections.sort(students,(x,y)->{
            return x.name.charAt(0)-y.name.charAt(0);
        });
        System.out.println(students);
    }
}
class Student implements Comparable<Student>{
    String name;
    Integer age;

    public Student() {
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
        return this.age-o.age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
